package com.zzy.blog.action;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.zzy.blog.json.jsonlib.House;
import com.zzy.blog.json.jsonlib.JsonUtils;
import com.zzy.blog.json.jsonlib.School;
import com.zzy.blog.json.jsonlib.Student;
import com.zzy.blog.json.jsonlib.Teacher;

/**
 * accept http request
 * @author eason
 *
 */
public class MonitorAction extends ActionSupport{
	
	public void monitor() throws IOException, ClassNotFoundException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		ObjectInputStream in = new ObjectInputStream(request.getInputStream());
		ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
		handleRequest(in, out); 
	}

	private void handleRequest(ObjectInputStream in, ObjectOutputStream out) throws IOException, ClassNotFoundException {
		String jsonString = (String) in.readObject();
		
		Map<String, Class> clazzMap = new HashMap<String, Class>();
		clazzMap.put("students", Student.class);
		clazzMap.put("teachers", Teacher.class);
		
		School school = JsonUtils.getObject(jsonString, School.class, clazzMap);
		System.out.println(school.getTeacher() instanceof Teacher);
		System.out.println(school.getStudents().get(0) instanceof Student);
		System.out.println(school.getTeachers().get(0) instanceof Teacher);
		System.out.println(school.getTeachers().get(0).getHouse() instanceof House);
		
		out.write("from idc".getBytes());
	}
}
